import Request from "@/plugins/request/js/index.js";
import { getToken,getShiroToken } from "@/utils/auth.js";
import systemInfoUtil from "@/utils/systemInfoUtil.js";
import configAll from "@/config/configAll";

let r = Request();

let reqId = r.interceptors.scoped.request.use(
  (config) => {
    if(config.platform) {
	  
      config.url = configAll.oldBaseUrl + config.instanceURL
	  
	  if(config.instanceURL=='platform-api/app/pay/payPrepayByType'){
		  conosle.log('header')
		 
	  }
      config.header['token'] = getShiroToken();
	   config.header['appType'] = 'pro'
      if (config.data && config.data["changeToken"]) {
        config.header["token"] = config.data["changeToken"];
      }
    }else if(config.platgroup){
		
		config.url = configAll.groupBaseUrl + config.instanceURL
		
		if (config.token) config.header["Authorization"] = config.token;
		else config.header["Authorization"] = getToken();
		if (config.data && config.data["changeToken"]) {
		  config.header["token"] = config.data["changeToken"];
		}
	}else {
      if (config.token) config.header["Authorization"] = config.token;
      else config.header["Authorization"] = getToken();
      if (config.data && config.data["changeToken"]) {
        config.header["Authorization"] = config.data["changeToken"];
      }
    }
	
    config.header["uniqueMinId"] = uni.getStorageSync("unique_min_id");
    if (uni.getStorageSync("openId"))
      config.header["openId"] = uni.getStorageSync("openId");
    config.header["env"] = systemInfoUtil.getEnv();
    config.header["minType"] = systemInfoUtil.getMinType();
	 config.header["minType"] = systemInfoUtil.getMinType();
    config.header["channel"] = systemInfoUtil.getMinChannel();
    // console.log(uni.getAccountInfoSync, "drrrrrrr");
    let accountInfo = undefined;
    if (uni.getAccountInfoSync) accountInfo = uni.getAccountInfoSync();
    // console.log(accountInfo);
    if (accountInfo) {
      config.header["appId"] = accountInfo.miniProgram.appId;
    } else {
      config.header["appId"] = configAll.appId;
    }
    // console.log(config,"interceptors.scoped请求")
    return config;
  },
  (err) => {
    return false;
  }
);

let repId = r.interceptors.scoped.response.use(
  (res, config) => {
    const { msg, data, code, message } = res;
    // code不为0或者200 则为业务异常，弹出msg
    // 0改成200
    if (code != 200) {
      if (code == 401) {
        console.log("toekn 时效");
      }else if(message=='很抱歉，所选入住离店时间包含无房日期'){
		  return res;
	  }else if(code == 40012){
		  // uni.showToast({
		  //   title: msg || message || "请求失败,请稍后再试",
		  //   icon: "none",
		  //   duration: 3000,
		  // });
	  } else {
        uni.showToast({
          title: msg || message || "请求失败,请稍后再试",
          icon: "none",
          duration: 3000,
        });
      }

      return Promise.reject(res, config);
    }
    return res;
  },
  (err, config) => {
	  console.log(err)
    return Promise.reject(err);
  }
);

// 卸载 私有 请求 拦截器
// instance.interceptors.scoped.request.eject(reqId)

export default r;
